package com.uade.dao;

import java.util.List;

import javax.ejb.Stateless;

import com.uade.entities.ArticuloEntity;

@Stateless
public class ArticuloDAOImpl extends
		AbstractBaseGenericDAOImpl<ArticuloEntity, Long> implements ArticuloDAO {

	public ArticuloDAOImpl() {
		super();
	}

	@Override
	public List<ArticuloEntity> buscarNombre(String nombre,String categoria) {

		StringBuffer query = new StringBuffer();
		query.append("SELECT a FROM ArticuloEntity a WHERE 1=1");
		if (categoria != null){
			query.append("AND a.categoria=?");
			query.append("AND a.nombre LIKE :code");
				return em
				.createQuery(
						query.toString())
				.setParameter("code", "%" + nombre + "%").setParameter(1, categoria).getResultList();
		}else{
			query.append("AND a.nombre LIKE :code");
			return em
			.createQuery(query.toString())
			.setParameter("code", "%" + nombre + "%").getResultList();
			
		}
	}

	@Override
	public List<ArticuloEntity> listarArticulos() {
		return (List<ArticuloEntity>) em.createQuery(
				"SELECT a FROM ArticuloEntity a ").getResultList();
	}

	@Override
	public List<ArticuloEntity> listarArticulosByRanking() {

		return (List<ArticuloEntity>) em.createQuery(
				"SELECT a FROM ArticuloEntity a ORDER BY a.ranking ")
				.getResultList();
	}


}